package com.bloveoacom.weichi.presenter;


import com.blankj.utilcode.util.StringUtils;
import com.bloveoacom.http.RxHelper;
import com.bloveoacom.observer.BaseSubscriber2;
import com.bloveoacom.util.GsonUtil;
import com.bloveoacom.weichi.bean.UserBean;
import com.bloveoacom.weichi.contract.LoginContract;

/**
 * author: zhu.px
 * e-mail: 1271901673@qq.com
 * time  : 2018/8/22
 * desc  : 我的
 */
public class LoginPresenter extends LoginContract.Presenter {


    @Override
    public void login(String phone, String password) {
        if(StringUtils.isEmpty(phone)){
            mView.showMessage("请输入用户名");
            return;
        }
        if(StringUtils.isEmpty(password)){
            mView.showMessage("请输入验证码");
            return;
        }
        BaseSubscriber2<Object> baseDialogObserver = mModel.login(phone, password).compose(RxHelper.<Object>getData()).subscribeWith(new BaseSubscriber2<Object>(mView) {

            @Override
            public void onNext(Object s) {
                String json = GsonUtil.GsonString(s);
                UserBean userBean = GsonUtil.GsonToBean(json,UserBean.class);
                mView.loginSuccess(userBean);
            }
        });
        addDispose(baseDialogObserver);
    }

    @Override
    public void sendCode(String phone, int type) {
        if(StringUtils.isEmpty(phone)){
            mView.showMessage("请输入用户名");
            return;
        }
        BaseSubscriber2<String> baseDialogObserver = mModel.sendCode(phone, type).compose(RxHelper.<String>getData()).subscribeWith(new BaseSubscriber2<String>(mView) {
            @Override
            public void onNext(String o) {
                super.onNext(o);
                mView.sendCode(o);
            }
        });
        addDispose(baseDialogObserver);
    }
}
